package addmi2_test;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;

public class index3 {

	protected Shell shell;
	private Text text;
	private Text text_1;
	private Text text_2;
	private Label lblNewLabel_3;
	private Text text_3;
	private Label lblNewLabel_4;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			index3 window = new index3();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(614, 439);
		shell.setText("解密");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(139, 49, 402, 35);
		
		text_1 = new Text(shell, SWT.BORDER);
		text_1.setBounds(139, 124, 402, 35);
		
		text_2 = new Text(shell, SWT.BORDER);
		text_2.setBounds(139, 200, 402, 35);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(21, 53, 76, 20);
		lblNewLabel.setText("暗文");
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setBounds(21, 127, 76, 20);
		lblNewLabel_1.setText("n");
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setBounds(21, 203, 76, 20);
		lblNewLabel_2.setText("d");
		
		Button btnNewButton = new Button(shell, SWT.NONE);

		btnNewButton.setBounds(267, 328, 98, 30);
		btnNewButton.setText("解密");
		
		lblNewLabel_3 = new Label(shell, SWT.NONE);
		lblNewLabel_3.setForeground(SWTResourceManager.getColor(255, 0, 0));
		lblNewLabel_3.setBounds(32, 333, 108, 25);
		
		text_3 = new Text(shell, SWT.BORDER);
		text_3.setBounds(139, 264, 402, 31);
		
		lblNewLabel_4 = new Label(shell, SWT.NONE);
		lblNewLabel_4.setBounds(21, 275, 76, 20);
		lblNewLabel_4.setText("解密后");
		
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String addmiStr = text.getText();
				String NStr = text_1.getText();
				String dStr = text_2.getText();
				
				if(addmiStr == null || "".equals(addmiStr.trim())) {
					lblNewLabel_3.setText("n没有值..");
					return;
				}
				
				if(NStr == null || "".equals(NStr.trim())) {
					lblNewLabel_3.setText("n没有值..");
					return;
				}
				
				if(dStr == null || "".equals(dStr.trim())) {
					lblNewLabel_3.setText("d没有值..");
					return;
				}
				
				int addmi = Integer.parseInt(addmiStr);
				int Nint = Integer.parseInt(NStr);
				int Dint = Integer.parseInt(dStr);
				int devs = dev(addmi,Nint, Dint);
				text_3.setText(devs+"");
			}
		});
	}
	
	private static int dev(int addmi, int n, int d) {
		int c = 1;
		for(int i =1;i<= d; i++) {
			c = c * addmi;
			if(c>n) {//防止这个数超出,如果数值太大可能会超出，即使是long整形的范围也不够
				c = c%n;
			}
		}
		return c;
	}
}
